package leecode

func LemonadeChange(bills []int) bool {
	billMap := make(map[int]int, len(bills)) //key为bill value为对应bill的数量
	for _, bill := range bills {
		switch bill {
		case 5:
			billMap[bill] = billMap[bill] + 1
		case 10:
			//10元只能给5元
			//判断5元的数量是否够
			_, ok := billMap[5]
			if !ok {
				return false
			} else {
				billMap[5] = billMap[5] - 1
				//10元纸币数量加1
				billMap[10] = billMap[10] + 1
			}
		case 20:
			//20 3张5元 一张10 2张5
			count10, _ := billMap[10]
			count5, _ := billMap[5]
			//优先找10元 , 当10元不够再去找5元
			if count10 >= 1 && count5 >= 1 {
				billMap[10] = billMap[10] - 1
				billMap[5] = billMap[5] - 1
			} else if count5 >= 3 {
				billMap[5] = billMap[5] - 3
			} else {
				return false
			}
		}
	}
	return true
}
